package xmap

// Merge 合并map, 将dsts合并到target
func Merge[K comparable, V any](target map[K]V, dsts ...map[K]V) map[K]V {
	if target == nil {
		target = make(map[K]V)
	}
	for _, dst := range dsts {
		if dst == nil {
			continue
		}
		for key, val := range dst {
			target[key] = val
		}
	}
	return target
}

// MergeWithSet 合并map, 将dsts合并到target 存在则合并到target，不存在则跳过
func MergeWithSet[K comparable, V any](target map[K]V, dsts ...map[K]V) map[K]V {
	if target == nil {
		return make(map[K]V)
	}
	for _, dst := range dsts {
		if dst == nil {
			continue
		}
		for key, val := range dst {
			if _, ok := target[key]; ok {
				target[key] = val
			}
		}
	}
	return target
}

// MergeWithoutSet 合并map, 将dsts合并到target 不存在则合并到target，存在则跳过
func MergeWithoutSet[K comparable, V any](target map[K]V, dsts ...map[K]V) map[K]V {
	if target == nil {
		target = make(map[K]V)
		for _, dst := range dsts {
			if dst == nil {
				continue
			}
			for key, val := range dst {
				target[key] = val
			}
		}
		return target
	}
	for _, dst := range dsts {
		if dst == nil {
			continue
		}
		for key, val := range dst {
			if _, ok := target[key]; !ok {
				target[key] = val
			}
		}
	}
	return target
}
